using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using Map2.MapFolder;

namespace Map2
{
    class MapPanel:Panel
    {
        private double zoom=1;
        private Size mapSize;
        private List<Building> buildings=new List<Building>();
        private Bitmap bmpBuildings,bmpUsers;
        private MouseEventMapPanel mapEvent;
        private bool editable =true;
        private Point drawPosition=new Point(0,0);
        private Size drawSize=new Size(640,480);

        public MapPanel() : base() {
            BackColor = Color.White;
            InitializeComponent();
            mapEvent = new MouseEventMapPanel(buildings);
        }

        public void setZoom(double z)
        {
            zoom = z;
            this.Width = (int)(mapSize.Width * zoom);
            this.Height = (int)(mapSize.Height * zoom);
            drawBuildings();
        }

        public void setMapSize(Size s)
        {
            mapSize = s;
            this.Width = (int)(s.Width*zoom);
            this.Height = (int)(s.Height*zoom);
        }

        public void setDrawProperties(Size s,Point p)
        {
            drawSize = s;
            drawPosition = p;
            drawBuildings();
        }

        public void setBuildings(List<Building> b)
        {
            buildings = b;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // MapPanel
            // 
            if (editable)
            {
                this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MapPanel_MouseDown);
                this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MapPanel_MouseMove);
                this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MapPanel_MouseClick);
                this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MapPanel_MouseUp);
            }
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.MapPanel_Paint);
            this.ResumeLayout(false);

            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        public void drawBuildings()
        {
            bmpBuildings = new Bitmap(drawSize.Width,drawSize.Height);
            Graphics g = Graphics.FromImage(bmpBuildings);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            foreach (Building b in buildings)
            {
                List<Point> drawPoints = new List<Point>();
                List<Point> drawBuildingPoints = new List<Point>();
                foreach (Point p in b.getPoints())
                {
                    int x = (int)(p.X * zoom) - drawPosition.X;
                    int y = (int)(p.Y * zoom) - drawPosition.Y;
                    if(x>0 && x<drawSize.Width && y>0 && y<drawSize.Height)
                        drawPoints.Add(new Point(x,y));
                    drawBuildingPoints.Add(new Point(x, y));
                }
                if (drawPoints.Count > 0 && drawBuildingPoints.Count>2)
                {
                    g.FillPolygon(Brushes.MidnightBlue, drawBuildingPoints.ToArray());
                }
                foreach (Point p in drawPoints)
                    g.DrawRectangle(new Pen(Color.Red, (int)(3 * zoom)), p.X - (int)(3 * zoom), p.Y - (int)(3 * zoom), (int)(6 * zoom), (int)(6 * zoom));
            }
            this.Invalidate();
        }

        public void drawUser(List<user> users)
        {
            bmpUsers = new Bitmap(drawSize.Width, drawSize.Height);
            Graphics g = Graphics.FromImage(bmpUsers);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            foreach (user u in users)
            {
                int x = (int)(u.getCoordinate().X * zoom) - drawPosition.X;
                int y = (int)(u.getCoordinate().Y * zoom) - drawPosition.Y;
                if (x > 0 && x < drawSize.Width && y > 0 && y < drawSize.Height)
                    g.FillEllipse(Brushes.Black, x, y, (int)(6*zoom), (int)(6*zoom));
            }
            this.Invalidate();
        }

        public void drawTextPosition(int x, int y)
        {
            Graphics g=Graphics.FromImage(bmpBuildings);
            g.DrawString("X=" + x + "\nY=" + y, new Font(FontFamily.GenericSerif, 12), Brushes.Black, 10, 10);
            this.Invalidate();
        }

        private void setContainerProperties(Point position, Size size)
        {
            drawPosition = position;
            drawSize = size;
            drawBuildings();
        }

        private void MapPanel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            if (bmpBuildings != null)
                g.DrawImage(bmpBuildings, drawPosition);
            
            if (bmpUsers != null)
                g.DrawImage(bmpUsers, drawPosition);

        }

        private void MapPanel_MouseDown(object sender, MouseEventArgs e)
        {
            mapEvent.setBuildings(buildings);
            mapEvent.mouseDown((int)(e.X / zoom),(int) (e.Y / zoom), e,this);
        }

        private void MapPanel_MouseMove(object sender, MouseEventArgs e)
        {
            mapEvent.mouseMove((int)(e.X / zoom), (int)(e.Y / zoom),this);
        }

        private void MapPanel_MouseUp(object sender, MouseEventArgs e)
        {
            mapEvent.mouseUp(this);
        }

        private void MapPanel_MouseClick(object sender, MouseEventArgs e)
        {
            mapEvent.mouseClick((int)(e.X / zoom), (int)(e.Y / zoom),MousePosition,e,this);
        }

        
    }
}
